//
//  BTEEmptyView.swift
//  BTE
//
//  Created by 张文 on 2025/8/30.
//

import UIKit

struct Empty<T> {
    public let Empty: T
    public init(_ dataSource: T) {
        Empty = dataSource
    }
}


extension NSObjectProtocol {
    var empty: Empty<Self> {
        return Empty(self)
    }
}


extension Empty where T : UIScrollView {
 
    var emptyDataSourceView: BTEEmptyView {
        return Empty.emptyView
    }
    
    var emptysuperView: UIScrollView {
        return emptyDataSourceView.superview as! UIScrollView
    }
    
    func hidden(){
        emptysuperView.bounces = true
        emptyDataSourceView.subviews.forEach{ $0.removeFromSuperview() }
        emptyDataSourceView.isHidden = true
    }
    
    func show(emptyImage image: UIImage,
              emptyTitle title: String?,
              buttonTitle: String? = nil,
              buttonImage: String = "",
              offsetY: CGFloat? = 0.0,
              topOffset: CGFloat = 0.0,
              canBunces: Bool? = false,
              action: @escaping ()->() = {  return }) {
        emptysuperView.bounces = canBunces ?? false
        emptyDataSourceView.isHidden = false
        emptyDataSourceView.image = image
        emptyDataSourceView.title = title
        emptyDataSourceView.buttonTitle = buttonTitle
        emptyDataSourceView.buttonImage = buttonImage
        emptyDataSourceView.action = action
        emptyDataSourceView.offsetY = offsetY ?? 0.0
        emptyDataSourceView.topOffset = topOffset
        emptyDataSourceView.layoutSubViews()
        
        Empty.layoutEmptyView()
    }

}





var kEmptyView = "emptyView"

public extension UIScrollView {
    
     var emptyView: BTEEmptyView {
        get {
            if let aValue = objc_getAssociatedObject(self, &kEmptyView) as? BTEEmptyView {
                return aValue
            }
            else {
                let empty = BTEEmptyView()
                empty.isHidden = true
                self.addSubview(empty)
                self.bringSubviewToFront(empty)
                self.emptyView = empty
                self.layoutEmptyView()
                return empty
            }
        }
        set {
            objc_setAssociatedObject(self, &kEmptyView, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    
     func layoutEmptyView(){
        emptyView.translatesAutoresizingMaskIntoConstraints = false
         emptyView.snp.remakeConstraints { make in
             make.left.right.bottom.equalToSuperview()
             make.top.equalTo(self).offset(self.emptyView.topOffset ?? 0)
             make.width.height.equalTo(self)
         }
         
    }
    
}





open class BTEEmptyView: UIView {
    
    // 占位图
    var image: UIImage?
    
    // 标题
    var title: String?
    
    // 按钮
    var buttonTitle: String?
    var buttonImage : String = ""
    
    // 回调
    var action: (()->())?
    
    // 偏移
    var offsetY: CGFloat = 0.0
    
    var topOffset: CGFloat? = 0.0 //距离父视图顶部距离
    
    var scrollView: UIScrollView?

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white
    }
    
    /// 布局显示
    func layoutSubViews(){
        
        let mainView = UIView()
        addSubview(mainView)
        mainView.snp.makeConstraints { make in
            make.centerY.equalTo(self).offset(self.offsetY)
            make.left.right.equalTo(self)
        }
        
        // 图片
        var lables: UILabel?
        if let i = image {
            let imageView = UIImageView.init(image: i)
            mainView.addSubview(imageView)
            imageView.snp.makeConstraints { make in
                make.top.equalTo(mainView)
                make.width.height.equalTo(i.size.width)
                make.centerX.equalTo(mainView)
                if title == nil {
                    make.bottom.equalTo(mainView)
                }
            }
            
            if title != nil {
                lables = UILabel.init()
        
                lables!.text = title
                lables!.textColor = .black
                lables!.font = UIFont.systemFont(ofSize: 14, weight: .regular)
                lables!.textAlignment = .center
                mainView.addSubview(lables!)
                lables!.snp.makeConstraints { make in
                    make.top.equalTo(imageView.snp.bottom).offset(24)
                    make.centerX.equalTo(mainView)
                    if self.buttonTitle == nil {
                        make.bottom.equalTo(mainView)
                    }
                }
            }
            
            if buttonTitle != nil {
                let button = UIButton()
                button.setBackgroundColor(.colorWithHex("#39B4E6")!)
                button.setTitle(title: buttonTitle!)
                button.setFont(UIFont.systemFont(ofSize: 16,weight: .bold))
                button.setTitleColor(.white)
                button.roundCorners(.allCorners, radius: 44/2)
                button.layer.borderColor = UIColor.colorWithHex("#6C2724")?.cgColor
                button.layer.borderWidth = 2
                mainView.addSubview(button)
                button.addTarget(self, action: #selector(doAction), for: .touchUpInside)
                button.snp.makeConstraints { make in
                    make.top.equalTo(lables!.snp.bottom).offset(14)
                    make.centerX.bottom.equalTo(mainView)
                    make.height.equalTo(44)
                    make.width.equalTo(175)
                }
            }
        }
    }
    
    required public init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc private func doAction(){
        action?()
    }

}

